﻿using System;
using System.Collections.Generic;
using System.Windows;
using BiljartHandicap.Views;

namespace BiljartHandicap.Util
{
    public static class NotificationUtil
    {
        private static readonly Queue<NotificationWindow> Queue = new Queue<NotificationWindow>();
        private static NotificationWindow _current = null;
        private const int NotificationDuration = 5000;

        /// <summary>
        /// Adds a notification
        /// </summary>
        /// <param name="message">Message to display</param>
        public static void AddNotification(string message)
        {
            var notificationWindow = new NotificationWindow();
            var notification = new Notification {MessageContent = {Text = message}};
            notificationWindow.Content = notification;
            notificationWindow.Closed += new EventHandler(NotificationWindowClosed);

            AddNotificationToQueue(notificationWindow);
        }

        /// <summary>
        /// Checks if there are items in the Queue and excecutes them
        /// </summary>
        private static void NotificationWindowClosed(object sender, EventArgs e)
        {
            _current = null;
            if (Queue.Count > 0)
            {
                _current = Queue.Dequeue();
                _current.Show(NotificationDuration);
            }
        }

        /// <summary>
        /// If there is no active notification the given one is showed
        /// else the Notification is added to the queue
        /// </summary>
        /// <param name="notificationWindow">NotificationWindow to show/add to the queue</param>
        private static void AddNotificationToQueue(NotificationWindow notificationWindow)
        {
            if (_current == null)
            {
                _current = notificationWindow;
                notificationWindow.Show(NotificationDuration);
            }
            else
            {
                Queue.Enqueue(notificationWindow);
            }
        }
    }
}
